﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongregacaoOs.Data;
using CongregacaoOs.Business;


namespace CongregacaoOs.WebForm.Cadastros
{
    public partial class Cidade : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            

            if (Session["DadosGerais"] == null) Response.Redirect("~/Cadastros/Account/Login.aspx");

            if (Session["TipoUsuario"].ToString() == "Local") Response.Redirect("~/Cadastros/Home.aspx");

            Page.Form.DefaultButton = btnBuscar.UniqueID;

            if (!IsPostBack)
            {
                AtualizarGridView();
                AtualizarDropDowListRegional();
                AtualizarDropRegionalFiltro();
            }
        }

        public void AtualizarGridView()
        {
            ccCidadeBus bus = new ccCidadeBus();
            ccCidade ent = new ccCidade();

            ent.nome = txtBuscarNome.Text;
            ent.idRegional = dropRegionalFiltro.SelectedValue != "" ? Convert.ToInt32(dropRegionalFiltro.SelectedValue) : 0;
            ent.idDistrital = dropDistritalFiltro.SelectedValue != "" ? Convert.ToInt32(dropDistritalFiltro.SelectedValue) : 0;
            ent.idAdm = dropAdmFiltro.SelectedValue != "" ? Convert.ToInt32(dropAdmFiltro.SelectedValue) : 0;

            gvCidade.DataSource = bus.CidadeSelectGrid(ent);
            gvCidade.DataBind();
        }

        public void AtualizarDropDowListRegional(int aID = 0)
        {
            ccRegionalBus bus = new ccRegionalBus();
            ccRegional ent = new ccRegional();
            dropRegional.DataSource = bus.RegionalSelect(ent);
            dropRegional.DataBind();
        }

        public void AtualizarDropDowListDistrital(int aID = 0)
        {
            ccDistritalBus bus = new ccDistritalBus();
            ccDistrital ent = new ccDistrital();
            ent.idRegional = aID;

            if (dropRegional.SelectedValue != "")
            {
                ent.idRegional = Convert.ToInt32(dropRegional.SelectedValue);
                dropDistrital.DataSource = bus.DistritalSelect(ent);
                dropDistrital.DataBind();
            }
            else
            {
                dropDistrital.Items.Clear();
            }
        }

        public void AtualizarDropDowListAdm(int aID = 0)
        {
            ccAdmBus bus = new ccAdmBus();
            ccAdm ent = new ccAdm();
            ent.idAdm = aID;

            ent.idRegional = dropRegional.SelectedValue != "" ? Convert.ToInt32(dropRegional.SelectedValue) : 0;
            if (dropDistrital.SelectedValue != "")
            {
                ent.idDistrital = Convert.ToInt32(dropDistrital.SelectedValue);
                dropAdm.DataSource = bus.AdmSelect(ent);
                dropAdm.DataBind();
            }
            else
            {
                dropAdm.Items.Clear();
            }

        }

        public void AtualizarDropRegionalFiltro()
        {
            using (ccRegionalBus bus = new ccRegionalBus())
            {
                ccRegional ent = new ccRegional();
                ent.idRegional = 0;
                ent.idPais = 0;
                ent.idEstado = 0;
                ent.nome = "";
                dropRegionalFiltro.DataSource = bus.RegionalSelect(ent);
                dropRegionalFiltro.DataBind();
            }

        }

        public void AtualizarDropDistritalFiltro()
        {
            using (ccDistritalBus bus = new ccDistritalBus())
            {
                ccDistrital ent = new ccDistrital();

                if (dropRegionalFiltro.SelectedValue != "")
                {
                    ent.idRegional = Convert.ToInt32(dropRegionalFiltro.SelectedValue);
                    dropDistritalFiltro.DataSource = bus.DistritalSelect(ent);
                    dropDistritalFiltro.DataBind();
                }
                else
                {
                    dropDistritalFiltro.Items.Clear();
                    dropAdmFiltro.Items.Clear();
                }

            }
        }

        public void AtualizarDropAdmFiltro()
        {
            if (dropDistritalFiltro.SelectedValue == "")
            {
                dropAdmFiltro.Items.Clear();
                return;
            }

            if (dropRegionalFiltro.SelectedValue != "" || dropDistritalFiltro.SelectedValue != "")
            {
                using (ccAdmBus bus = new ccAdmBus())
                {
                    ccAdm ent = new ccAdm();

                    if (dropRegionalFiltro.SelectedValue != "")
                        ent.idRegional = dropRegionalFiltro.SelectedValue != "" ? Convert.ToInt32(dropRegionalFiltro.SelectedValue) : 0;

                    if (dropDistritalFiltro.SelectedValue != "")
                    {
                        ent.idDistrital = dropDistritalFiltro.SelectedValue != "" ? Convert.ToInt32(dropDistritalFiltro.SelectedValue) : 0;

                        dropAdmFiltro.DataSource = bus.AdmSelect(ent);
                        dropAdmFiltro.DataBind();
                    }
                    else
                    {
                        dropAdmFiltro.Items.Clear();
                    }


                }
            }
            else
                dropAdmFiltro.Items.Clear();
        }




        protected void gvCidade_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EDITAR" || e.CommandName == "EXCLUIR")
            {
                GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

                int index = row.RowIndex;

                hdID.Value = gvCidade.Rows[index].Cells[1].Text.Trim();
                HiddenField hdIdAdm = (gvCidade.Rows[index].FindControl("hdIdAdm") as HiddenField);
                HiddenField hdIdRegional = (gvCidade.Rows[index].FindControl("hdIdRegional") as HiddenField);
                HiddenField hdIdDistrital = (gvCidade.Rows[index].FindControl("hdIdDistrital") as HiddenField);

                if (e.CommandName == "EDITAR")
                {
                    hfIndexAccordion.Value = "1";
                    txtNome.Text = gvCidade.Rows[index].Cells[2].Text.Trim();

                    AtualizarDropDowListRegional();
                    dropRegional.SelectedValue = hdIdRegional.Value;

                    if (hdIdDistrital.Value != "0")
                    {
                        AtualizarDropDowListDistrital();
                        dropDistrital.SelectedValue = hdIdDistrital.Value;
                    }

                    AtualizarDropDowListAdm();
                    dropAdm.SelectedValue = hdIdAdm.Value;

                }
                else if (e.CommandName == "EXCLUIR")
                {
                    ccCidadeBus bus = new ccCidadeBus();
                    bus.CidadeDelete(Convert.ToInt32(hdID.Value));
                }
            }

            AtualizarGridView();
        }

        protected void gvCidade_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AtualizarGridView();
            gvCidade.PageIndex = e.NewPageIndex;
            gvCidade.DataBind();
        }

        protected void dropRegional_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropDowListDistrital();
        }

        protected void gvCidade_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgEditar = (e.Row.FindControl("imgEditar") as ImageButton);
                imgEditar.CommandArgument = e.Row.RowIndex.ToString();

                ImageButton imgDelete = (e.Row.FindControl("imgDelete") as ImageButton);
                imgDelete.CommandArgument = e.Row.RowIndex.ToString();

                imgDelete.Attributes.Add("onclick", "javascript:return " +
                        "confirm('Confirmar exclusão do registro: " +
                        DataBinder.Eval(e.Row.DataItem, "Cidade") + "?')");
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {

            try
            {
                ccCidadeBus bus = new ccCidadeBus();
                ccCidade ent = new ccCidade();

                ent.nome = txtNome.Text.Trim();
                ent.idAdm = dropAdm.SelectedValue != "" ? Convert.ToInt32(dropAdm.SelectedValue) : 0;
                ent.idRegional = dropRegional.SelectedValue != "" ? Convert.ToInt32(dropRegional.SelectedValue) : 0;
                ent.idDistrital = dropDistrital.SelectedValue != "" ? Convert.ToInt32(dropDistrital.SelectedValue) : 0;

                if (hdID.Value != "") ent.idCidade = Convert.ToInt32(hdID.Value);

                bus.CidadeSaveOrUpdate(ent);
                hfIndexAccordion.Value = "0";

                MessageWindow.MessageType = true;
                MessageWindow.Texto = "Registro salvo com sucesso!!!";
                MessageWindow.ShowWindow();

                AtualizarGridView();
            }
            catch (Exception)
            {

                MessageWindow.MessageType = false;
                MessageWindow.Texto = "Houve um erro ao salvar o registro. Tente novamente ou contate o suporte!!!";
                MessageWindow.ShowWindow();
            }

        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            hfIndexAccordion.Value = "0";
            hdID.Value = "";
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

        protected void btnAdicionarNovo_Click(object sender, EventArgs e)
        {

            AtualizarDropDowListRegional();
            hdID.Value = "";
            Functions.LimparCampos(this);
            hfIndexAccordion.Value = "1";
        }

        protected void dropDistrital_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropDowListAdm();
        }

        protected void dropRegional_DataBound(object sender, EventArgs e)
        {
            dropRegional.Items.Insert(0, new ListItem("Escolha a Regional", String.Empty));
        }

        protected void dropDistrital_DataBound(object sender, EventArgs e)
        {
            dropDistrital.Items.Insert(0, new ListItem("Escolha a Distrital", String.Empty));
        }

        protected void dropAdm_DataBound(object sender, EventArgs e)
        {
            dropAdm.Items.Insert(0, new ListItem("Escolha a Administração", String.Empty));
        }

        protected void dropRegionalFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropDistritalFiltro();
            AtualizarGridView();
        }

        protected void dropRegionalFiltro_DataBound(object sender, EventArgs e)
        {
            dropRegionalFiltro.Items.Insert(0, new ListItem("Escolha a Regional", String.Empty));
        }

        protected void dropDistritalFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropAdmFiltro();
            AtualizarGridView();
        }

        protected void dropDistritalFiltro_DataBound(object sender, EventArgs e)
        {
            dropDistritalFiltro.Items.Insert(0, new ListItem("Escolha a Distrital", String.Empty));
        }

        protected void dropAdmFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

        protected void dropAdmFiltro_DataBound(object sender, EventArgs e)
        {
            dropAdmFiltro.Items.Insert(0, new ListItem("Escolha a Administração", String.Empty));
        }
    }
}